<template>
  <div class="main-container">
    <el-card class="box-card !border-none" shadow="never">
      <div class="flex justify-between items-center">
        <span class="text-lg">{{ pageName }}</span>
      </div>

      <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
        <el-form :inline="true" :model="memberDivvyTable.searchParam" ref="searchFormRef">
          <el-form-item label="分红日期" prop="create_time">
            <el-date-picker
              v-model="memberDivvyTable.searchParam.create_time"
              type="datetimerange"
              value-format="YYYY-MM-DD HH:mm:ss"
              start-placeholder="起始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="loadMemberDivvyList()">搜索</el-button>
            <el-button @click="resetForm(searchFormRef)">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <div class="mt-[10px]">
        <el-table
          :data="memberDivvyTable.data"
          size="large"
          v-loading="memberDivvyTable.loading"
        >
          <template #empty>
            <span>{{ !memberDivvyTable.loading ? "暂无数据" : "" }}</span>
          </template>
          <el-table-column
            prop="member.username"
            label="用户名称"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            prop="area_performance"
            label="团队业绩"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            prop="area_contribution"
            label="团队总贡献分"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            prop="member_contribution"
            label="当前用户贡献分"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            prop="contribution_bl"
            label="当前用户贡献分占比"
            min-width="120"
          >
            <template #default="{ row }"> {{ Number((row.contribution_bl * 100).toFixed(2)) }}% </template>
          </el-table-column>
          <el-table-column
            prop="amount"
            label="分红金额"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column prop="dividend_rate" label="分红系数" min-width="120">
            <template #default="{ row }"> {{ row.dividend_rate }}% </template>
          </el-table-column>
          <el-table-column
            prop="dividend_date"
            label="分红日期"
            min-width="120"
            show-overflow-tooltip
          />
        </el-table>
        <div class="mt-[16px] flex justify-end">
          <el-pagination
            v-model:current-page="memberDivvyTable.page"
            v-model:page-size="memberDivvyTable.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="memberDivvyTable.total"
            @size-change="loadMemberDivvyList()"
            @current-change="loadMemberDivvyList"
          />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { getMemberDividendTwoList } from "@/app/api/distributor";
import { FormInstance } from "element-plus";
import { useRoute } from "vue-router";
const route = useRoute();
const pageName = route.meta.title;

const memberDivvyTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  searchParam: {
    create_time: "",
  },
});

const searchFormRef = ref<FormInstance>();

/**
 * 获取成员分红记录列表
 */
const loadMemberDivvyList = (page: number = 1) => {
  memberDivvyTable.loading = true;
  memberDivvyTable.page = page;

  getMemberDividendTwoList({
    page: memberDivvyTable.page,
    limit: memberDivvyTable.limit,
    ...memberDivvyTable.searchParam,
  })
    .then((res) => {
      memberDivvyTable.loading = false;
      memberDivvyTable.data = res.data.data;
      memberDivvyTable.total = res.data.total;
    })
    .catch(() => {
      memberDivvyTable.loading = false;
    });
};
loadMemberDivvyList();

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  loadMemberDivvyList();
};
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
  word-break: break-all;
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
